﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Controls;
using com.zjgis.baseTools;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WindowsApplication1.dialog;
using System.Configuration;
using ESRI.ArcGIS.Geometry;
using com.zjgis.arcengineutility;
using System.Data.Common;
using pnmodel;
using com.zjgis.control;
using System.IO;

namespace WindowsApplication1
{
    public class Identify : BaseDrawRect
    {
        public Identify()
        {
            
        }
        public void ShowForm()
        {
            QueryResultPanel.Instance.SetVisible();
        }

        public override void OnMouseUp(int button, int shift, int x, int y)
        {
            base.OnMouseUp(button, shift, x, y);
            IGeometry geom = base.m_geom;            
            ISpatialFilter sFilter = new SpatialFilterClass();
            sFilter.Geometry = geom;
            sFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            QueryResultPanel.Instance.Clear();
            IMapControl2 c2 = m_hookHelper.Hook as IMapControl2;
            try
            {
                string coords = "";
                if (geom is IEnvelope)
                {
                    IPointCollection polygon = new PolygonClass() as IPointCollection;
                    IPoint p1 = new PointClass();
                    p1.X = geom.Envelope.XMin;
                    p1.Y = geom.Envelope.YMin;
                    object obj = Type.Missing;
                    polygon.AddPoint(p1, ref obj, ref obj);
                    p1 = new PointClass();
                    p1.X = geom.Envelope.XMin;
                    p1.Y = geom.Envelope.YMax;
                    polygon.AddPoint(p1, ref obj, ref obj);
                    p1 = new PointClass();
                    p1.X = geom.Envelope.XMax;
                    p1.Y = geom.Envelope.YMax;
                    polygon.AddPoint(p1, ref obj, ref obj);
                    p1 = new PointClass();
                    p1.X = geom.Envelope.XMax;
                    p1.Y = geom.Envelope.YMin;
                    polygon.AddPoint(p1, ref obj, ref obj);
                    p1 = new PointClass();
                    p1.X = geom.Envelope.XMin;
                    p1.Y = geom.Envelope.YMin;
                    polygon.AddPoint(p1, ref obj, ref obj);
                    coords = PnGeometryUtility.getGeoSQL(polygon as IGeometry);
                }
                else
                {
                    coords = PnGeometryUtility.getGeoSQL(geom);
                }
                string sql = "select a.*,b.shape.get_gtype() as shapetype,b.shape.Get_WKT() as shape from 用电建筑信息 a,建筑 b where SDO_RELATE(b.shape,SDO_GEOMETRY('" + coords + "'),'mask=ANYINTERACT') = 'TRUE' and a.建筑统一编码 = b.buildingid";
                QueryResultPanel.Instance.ShowQueryResult(sql, c2);
                QueryResultPanel.Instance.treeView1.Nodes[0].Expand();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                File.AppendAllText("log.txt",ex.Message);
                File.AppendAllText("log.txt", ex.StackTrace);
            }
            //qform.AddLayerAndQueryFilter(sFilter, Form1.社区图层, ConfigurationManager.AppSettings["社区名称"]);
            //qform.AddLayerAndQueryFilter(sFilter, Form1.街道图层, ConfigurationManager.AppSettings["街道名称"]);
            ShowForm();            
        }
    }
}
